package gUtil.fileUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * 
 * @author admin
 *	fastdfs工具包
 */
@Component
public class FastDFSUtil  {
	static TrackerServer trackerServer = null;
	static StorageClient1 client = null;
	
	static {
		try {
			ClientGlobal.init("fastdfs-client.properties");
			TrackerClient tracker = new TrackerClient();
		    trackerServer = tracker.getConnection();
		    //定义Storage客户端
		    StorageServer storageServer = null;
			client = new StorageClient1(trackerServer, storageServer);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	/**
	 * 上传文件方法
	 * System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
	 * System.out.println("charset=" + ClientGlobal.g_charset);
	 */
	public FileSystem toUpload(MultipartFile file) {
		FileSystem fileSystem = new FileSystem();
		//用spring 获取运行时类路径路径
		String filePath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
		try {
		    String originalFilename = file.getOriginalFilename();
		    String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
			String newFileName = UUID.randomUUID().toString() + extension;
			File f = new File(filePath + newFileName);
			file.transferTo(f);
			NameValuePair nvp [] =null;
			String local_filename = f.getAbsolutePath();
			//上传到文件系统
			String fileId = client.upload_file1(local_filename, null, nvp);
			//文件在文件系统中的路径 
			fileSystem.setFilePath("http://47.106.108.64:8888/" + fileId);
			fileSystem.setFileId(fileId);
			long size = file.getSize();//文件大小
			fileSystem.setFileSize(size);
			String contentType = file.getContentType();
			//文件类型
			fileSystem.setFileType(contentType);
			//文件名称
			if (fileSystem.getFileName()== null|| fileSystem.getFileName().equals("")) {
				//如果没有传入文件名称则存储文件的原始名称
				String[] split = fileId.split("/");
				fileSystem.setFileName(split[4]);
			}//删除web服务器上的文件
			f.delete();
		} catch (Exception ex) {
		     ex.printStackTrace();
		 }
		 return fileSystem; 
	}
	/**
	 * 下载文件
	 * fileId:  group1/M00/00/00/rBEqP13FdauAaa3HAACWs6x-odk462.jpg
	 * path: 文件下载位置
	 * 
	 * @throws MyException 
	 * @throws IOException 
	 */
	public void downLoad(String fileId,String path) throws IOException, MyException {
		byte[] result = client.download_file1(fileId);
		System.out.println(result);
		File file = new File(path);
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		fileOutputStream.write(result);
		fileOutputStream.close();
	}
	/**
	 * 查询文件
	 * @throws IOException 
	 * path:  group1/M00/00/00/rBEqP13FdauAaa3HAACWs6x-odk462.jpg
	 */
	public FileInfo query(String path) throws Exception {	      
	      FileInfo info1 = client.query_file_info1(path);
	      trackerServer.close();
	      return info1;
	}
}
